
let layer, element

$(function () {
    // 默认显示账号密码登录
    $('.phone').hide()
    layui.use(['table', 'form', 'element', 'layer'], function () {
        layer = layui.layer, element = layui.element
    })
})

/**
 * 获取手机验证界面的验证码
 */
function getVerification() {
    let stuMobile = $.trim($('#phone-stuMobile').val())
    if (stuMobile === null || stuMobile === '') {
        layer.tips('登录手机不能为空', '#phone-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!reg.test(stuMobile)) {
        layer.tips('登录手机格式不正确', '#phone-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    let student = {stuMobile: stuMobile}
    post(api.GetVerification, student).then(res => {
        $('.phone-code-box a').attr("disabled", true); //设置成灰色不可点击
        $('.phone-code-box a').css("pointer-events", "none");  //设置鼠标事件不可用
        $('.phone-code-box a').addClass('a-not-click')
        let time = 30
        $('.phone-code-box a').text(time + '秒后可用')
        let id = setInterval(function () {
            time--
            if (time <= 1) {
                $('.phone-code-box a').text('获取验证码')
                $('.phone-code-box a').attr("disabled", false);
                $('.phone-code-box a').css("pointer-events", "auto");
                $('.phone-code-box a').removeClass('a-not-click')
                clearInterval(id)
                return
            }
            $('.phone-code-box a').text(time + '秒后可用')
        }, 1000)
    })
}


/**
 * 绑定手机号进行微信注册
 */
function saveBindLogin() {
    let stuMobile = $.trim($('#phone-stuMobile').val())
    let code = $.trim($('#phone-code').val())
    if (stuMobile === null || stuMobile === '') {
        layer.tips('登录手机不能为空', '#phone-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!reg.test(stuMobile)) {
        layer.tips('登录手机格式不正确', '#phone-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    if (code === null || code === '') {
        layer.tips('验证码不能为空', '#phone-code', {tips: [3, '#202d4a']});
        return false
    }
    let studentEntity = {stuMobile: stuMobile, unionid: $('#unionid').val(), code: code}
    post(api.BindMobileAndUnionId, studentEntity).then(res => {
        if (res.code === '000000') {
            layer.confirm('您的初始密码为：' + (res.data.password), {
                btn: ['已知晓'] //按钮
            }, function () {
                location.href = '/'
            });
        } else {
            layer.msg('手机号或验证码错误。', {icon: 5});
        }
    })
}